package cn.pid21.client.request;

import cn.pid21.client.exception.DataReportClientException;
import cn.pid21.client.model.ParamType;
import cn.pid21.client.model.DataReportService;
import cn.pid21.client.util.RequestBodyUtil;
import cn.pid21.client.util.RequestUtil;
import okhttp3.Headers;
import okhttp3.Request;

/**
 * CstrRegisterRequest
 *
 * @author 郭志斌
 * @version 1.1.0
 * <b>Creation Time:</b> 2020/7/31 11:08
 */
public class StatisticalRegisterRequest implements DataReportRequest {

    /**
     * 请求参数
     */
    private String params;

    /**
     * 参数类型 json/xml
     */
    private ParamType paramType;

    private StatisticalRegisterRequest(){}

    public static StatisticalRegisterRequest builder(){
        return new StatisticalRegisterRequest();
    }

    /**
     * 设置请求参数
     *
     * @param params 请求参数
     * @return CstrRegisterRequest
     */
    public StatisticalRegisterRequest params(String params){
        this.params = params;
        return this;
    }

    /**
     * 设置请求参数类型
     *
     * @param paramType 请求参数类型
     * @return CstrRegisterRequest
     */
    public StatisticalRegisterRequest paramType(ParamType paramType){
        this.paramType = paramType;
        return this;
    }

    @Override
    public Request build(String clientId, String secret) {
        Headers headers = RequestUtil.headers(DataReportRequestHeader.builder().httpBasic(clientId,secret));
        switch (paramType){
            case JSON:
                return RequestUtil.post(headers,
                        RequestBodyUtil.json(params),
                        DataReportRequest.DATA_REPORT_SERVICE_GATEWAY_URL,
                        service().getName());
            case XML:
                return RequestUtil.post(headers,
                        RequestBodyUtil.xml(params),
                        DataReportRequest.DATA_REPORT_SERVICE_GATEWAY_URL,
                        service().getName());
            default:
                throw new DataReportClientException("请求参数类型异常");
        }
    }

    @Override
    public DataReportService service() {
        return DataReportService.STATISTICAL_INFO_REGISTER;
    }
}
